﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tools.Linq;
using MobiMenu.Restaurant.Web.Utility;
using System.Collections.Specialized;
using System.Linq.Expressions;

namespace MobiMenu.Restaurant.Web
{
    /// <summary>
    ///Class1 的摘要说明
    /// </summary>
    public class DishesService : BaseRESTService<Dish>
    {
        public DishesService()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        protected override Expression<Func<Dish, bool>> GetWhereExpression(NameValueCollection nameValues)
        {
            var queryBuilder = QueryBuilder.Create<Dish>();
            
            int[] ids;
            if (RequestHelper.GetInt32Array(nameValues, "Id", out ids))
            {
                queryBuilder = queryBuilder.In(c => c.Id, ids);
            }

            double startPrice;
            double endPrice;
            if (RequestHelper.GetDoubleRange(nameValues, "Price", out startPrice, out endPrice))
            {
                queryBuilder = queryBuilder.Between(c => c.Price, startPrice, endPrice);            
            }

            if (RequestHelper.GetDoubleRange(nameValues, "DiscountPrice", out startPrice, out endPrice))
            {
                queryBuilder = queryBuilder.Between(c => c.DiscountPrice, startPrice, endPrice);
            }

            DishType[] dishTypes;
            if (RequestHelper.GetEnumArray<DishType>(nameValues, "DishType", out dishTypes))
            {
                queryBuilder = queryBuilder.In(c => c.DishType, dishTypes);
            }

            string[] names;
            if (RequestHelper.GetStringArray(nameValues, "Name", out names))
            {
                queryBuilder = queryBuilder.In(c => c.Name, names);
            }

            bool showInMenu;
            if (RequestHelper.GetBool(nameValues, "ShowInMenu", out showInMenu))
            {
                queryBuilder = queryBuilder.Equals(c => c.ShowInMenu, showInMenu);
            }

            bool useDiscountPrice;
            if (RequestHelper.GetBool(nameValues, "UseDiscountPrice", out useDiscountPrice))
            {
                queryBuilder = queryBuilder.Equals(c => c.UseDiscountPrice, useDiscountPrice);
            }

            return queryBuilder.Expression;
        }

        protected override bool ProcessOrder(string sortField, bool isDesc, ref IQueryable<Dish> query)
        {
            if (string.IsNullOrEmpty(sortField))
            {
                return false;
            }

            if (sortField.Equals("Id", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Dish, int>> expr = c => c.Id;
                query = CommonHelper.QueryOrder<Dish, int>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("DiscountPrice", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Dish, double?>> expr = c => c.DiscountPrice;
                query = CommonHelper.QueryOrder<Dish, double?>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("DishType", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Dish, DishType>> expr = c => c.DishType;
                query = CommonHelper.QueryOrder<Dish, DishType>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("Name", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Dish, string>> expr = c => c.Name;
                query = CommonHelper.QueryOrder<Dish, string>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("Price", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Dish, double?>> expr = c => c.Price;
                query = CommonHelper.QueryOrder<Dish, double?>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("ShowInMenu", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Dish, bool>> expr = c => c.ShowInMenu;
                query = CommonHelper.QueryOrder<Dish, bool>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("UseDiscountPrice", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Dish, bool>> expr = c => c.UseDiscountPrice;
                query = CommonHelper.QueryOrder<Dish, bool>(query, expr, isDesc);
                return true;
            }

            return false;
        }
        
        protected override bool GetDeleteWhereExpression(RESTContext<Dish> context,
            out Expression<Func<Dish, bool>> expr)
        {
            expr = null;
            if (context == null || context.RequestData == null
                || context.RequestData.DeleteIds == null || context.RequestData.DeleteIds.Count == 0)
            {
                return false;
            }

            var queryBuilder = QueryBuilder.Create<Dish>();
            queryBuilder = queryBuilder.In(c => c.Id, context.RequestData.DeleteIds.ToArray());
            expr = queryBuilder.Expression;
            return true;
        }

        protected override bool UpdateEntity(NameValueCollection nameValues, Dish item)
        {
            bool changed = false;
            double dp;
            if (RequestHelper.GetDouble(nameValues, "DiscountPrice", out dp))
            {
                item.DiscountPrice = dp;
                changed = true;
            }

            double price;
            if (RequestHelper.GetDouble(nameValues, "Price", out price))
            {
                item.Price = price;
                changed = true;
            }

            string description;
            if (RequestHelper.GetString(nameValues, "Description", out description))
            {
                item.Description = description;
                changed = true;
            }

            string imagePath;
            if (RequestHelper.GetString(nameValues, "ImagePath", out imagePath))
            {
                item.ImagePath = imagePath;
                changed = true;
            }

            string name;
            if (RequestHelper.GetString(nameValues, "Name", out name))
            {
                item.Name = name;
                changed = true;
            }

            DishType dishType;
            if (RequestHelper.GetEnum<DishType>(nameValues, "DishType", out dishType))
            {
                item.DishType = dishType;
                changed = true;
            }

            bool showInMenu;
            if (RequestHelper.GetBool(nameValues, "ShowInMenu", out showInMenu))
            {
                item.ShowInMenu = showInMenu;
                changed = true;
            }

            bool useDiscountPrice;
            if (RequestHelper.GetBool(nameValues, "UseDiscountPrice", out useDiscountPrice))
            {
                item.UseDiscountPrice = useDiscountPrice;
                changed = true;
            }

            return changed;
        }

        protected override bool GetUpdateWhereExpression(NameValueCollection nameValues, out Expression<Func<Dish, bool>> expr)
        {
            var queryBuilder = QueryBuilder.Create<Dish>();
            int id;
            if (RequestHelper.GetInt32(nameValues, "Id", out id))
            {
                queryBuilder = queryBuilder.Equals(c => c.Id, id);
                expr = queryBuilder.Expression;
                return true;
            }

            expr = null;
            return false;
        }
    }
}